import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:hi_device_util/hi_device_util.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _deviceInfo = "获取设备信息失败";
  String _contacts = "获取联系人信息失败";
  String _messages = "获取短信信息失败";
  String _appList = "获取app列表失败";

  @override
  void initState() {
    super.initState();
    getDeviceInfo();
    getContacts();
    getMessages();
    getAppList();
  }

  Future<void> getDeviceInfo() async {
    String info;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      info =
          await HiDeviceUtil.getDeviceInfo ?? 'Unknown platform version';
      print("设备信息--->$info");
    } on PlatformException catch(e){
      info = '$e';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _deviceInfo = info;
    });
  }

  Future<void> getContacts() async {
    String info;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      info =
          await HiDeviceUtil.getContacts ?? 'Unknown platform version';
      print("联系人信息--->$info");
    } on PlatformException catch(e){
      info = '$e';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _contacts = info;
    });
  }

  Future<void> getMessages() async {
    String info;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      info =
          await HiDeviceUtil.getMessage ?? 'Unknown platform version';
      print("短信信息--->$info");
    } on PlatformException catch(e) {
      info = '$e';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _messages = info;
    });
  }

  Future<void> getAppList() async {
    String info;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      info =
          await HiDeviceUtil.getAppList ?? 'Unknown platform version';
    } on PlatformException {
      info = '获取App列表失败';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _appList = info;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: ListView(
            children: [
              Text('设备信息: $_deviceInfo\n',style: TextStyle(color: Colors.red),),
              Text('联系人信息: $_contacts\n',style: TextStyle(color: Colors.yellow),),
              Text('短信信息: $_messages\n',style: TextStyle(color: Colors.black),),
              Text('App列表: $_appList\n',style: TextStyle(color: Colors.green),),
            ],
          ),
        ),
      ),
    );
  }
}
